
package online.chenlu.blog.configurer;

import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;

/**
 * Created by chenlu on 2019/03/27.
 */
@Configuration
@Profile({"dev", "test", "home", "docker"})
@EnableSwagger2
public class SwaggerConfig {
    /**
     * @return api
     */
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("API接口文档").apiInfo(apiInfo())
                .useDefaultResponseMessages(false)
                .ignoredParameterTypes(ModelMap.class, HttpServletRequest.class,
                        HttpServletResponse.class, BindingResult.class)
                .select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.any()).paths(PathSelectors.regex("^(?!noauth).*$"))
                .build().securitySchemes(securitySchemes()).securityContexts(securityContexts())
                //@formatter:off
                .tags(
                        new Tag("securityController", "Security api服务."),
                        new Tag("TeamArea", "TeamArea api服务."),
                        new Tag("TeamExam", "TeamExam api服务.")
                );
        //@formatter:on
    }

    /**
     * @return List<ApiKey> securitySchemes
     */
    private List<ApiKey> securitySchemes() {
        return newArrayList(new ApiKey("Authorization", "Authorization", "header"));
    }

    /**
     * @return securityContexts
     */
    private List<SecurityContext> securityContexts() {
        return newArrayList(SecurityContext.builder().securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!noauth).*$")).build());
    }

    /**
     * @return 默认的授权模式
     */
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global",
                "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return newArrayList(new SecurityReference("Authorization", authorizationScopes));
    }

    /**
     * @return 用来创建该Api的基本信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("companyName-projectName 接口文档").description("公司简称项目名称api接口信息.")
                .termsOfServiceUrl("http://www.minstone.com/")
                .contact(new Contact("明动软件", "http://www.minstone.com/", "admin@minstone.com"))
                .version("1.0").license(null).build();
    }
}
